/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/27 23:57
 */
package org.mechanic.fund.service.product.impl;

import com.alibaba.fastjson.JSONObject;
import org.mechanic.core.Constants;
import org.mechanic.fund.domain.system.manager.User;
import org.mechanic.fund.domain.utils.mapper.DtoBeanMapper;
import org.mechanic.fund.repository.system.UserRepository;
import org.mechanic.fund.service.product.IUserService;
import org.mechanic.fund.service.wechat.IWcAccessApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息服务实现
 *
 * @author mechanic
 * @version 0.0.1
 */
@Service
public class UserService implements IUserService {

    private static Logger logger = LoggerFactory.getLogger(UserService.class);

    private final UserRepository userRepository;
    private final IWcAccessApiService apiService;

    @Autowired
    public UserService(UserRepository userRepository, IWcAccessApiService apiService) {
        this.userRepository = userRepository;
        this.apiService = apiService;
    }

    /**
     * 返回匹配用户编号的用户信息
     *
     * @param id 用户编号
     * @return 用户信息
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public User findById(String id) {
        return this.userRepository.findOne(id);
    }

    /**
     * 返回匹配登录账号的用户信息
     *
     * @param account 登录账号
     * @return 用户信息
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public User findByAccount(String account) {
        return this.userRepository.findByAccount(account);
    }

    /**
     * 返回用户信息分页列表
     *
     * @param pageRequest 分页信息
     * @return 用户信息分页列表
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public Page<User> getList(PageRequest pageRequest) {
        return this.userRepository.findAll(pageRequest);
    }

    /**
     * 删除匹配用户编号的用户信息
     *
     * @param id 用户编号
     */
    @Override
    public void delete(String id) {
        try {
            this.userRepository.delete(id);
        } catch (EmptyResultDataAccessException e) {
            logger.info(id + "不存在");
        }
    }

    /**
     * 批量保存用户信息
     *
     * @param users           用户信息集合
     * @param operationUserId 操作该记录的用户主键编号
     */
    @Override
    public void save(List<User> users, String operationUserId) {
        this.userRepository.save(DtoBeanMapper.setOperationToList(users, operationUserId));
    }

    /**
     * 保存用户信息
     *
     * @param user            用户信息
     * @param operationUserId 操作该记录的用户主键编号
     */
    @Override
    @Transactional
    public Map save(User user, String operationUserId) {
        User saved = this.userRepository.save(DtoBeanMapper.setOperationToDomain(user, operationUserId));
        Map<String, String> wxUser = new LinkedHashMap<>();
        wxUser.put("userid", saved.getWxUserId());
        wxUser.put("name", saved.getName());
        wxUser.put("department", saved.getDepartment().getId());
        wxUser.put("position", saved.getWxPosition());
        wxUser.put("mobile", saved.getWxMobile());
        wxUser.put("gender", saved.getWxGender().toString());
        wxUser.put("email", saved.getWxEmail());
        wxUser.put("weixinid", saved.getWeiXinId());
        //wxUser.put("avatar_mediaid", saved.getWeiXinId());
        wxUser.put("extattr", saved.getWeiXinId());
        //wcDepartment.put("weixinid", saved.getWeiXinId());
        return this.apiService.saveUser(JSONObject.toJSONString(wxUser, true));
    }
}
